package com.exsun.ex1077.ui.fragment.vehicletrack

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.MapVehicle
import com.exsun.ex1077.network.entity.VehicleTrack
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.extend.getTodayDateTimeStr
import com.zhangping.fastjetpack.extend.toDateStr
import com.zhangping.fastjetpack.extend.with000000
import com.zhangping.fastjetpack.extend.with235959
import java.util.*

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/10 10:54
 */
class VehicleTrackViewModel: BaseViewModel() {
    val model = ObservableField<MapVehicle>()
    val customTime = BooleanObservableField()
    val startTime = StringObservableField("")
    val endTime = StringObservableField("")

    val resultLiveData = MutableLiveData<ResultState<List<VehicleTrack>?>>()

    fun loadData() {
        val queryMap = JSONObject().apply {
            put("dvo", model.get()?.dvo ?: "")
            if (customTime.get()){
                put("startTime", startTime.get())
                put("endTime", endTime.get())
            }else{
                put("startTime", Date().toDateStr(true))
                put("endTime", Date().toDateStr(with235959 = true))
            }
        }
        request({ apiService.vehicleTrackList(queryMap)}, resultLiveData, true)
    }

}